package com.benshou.bcss.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.benshou.bcss.common.db.BaseEntity;
import com.benshou.bcss.common.db.BusinessNoLong;
import com.benshou.bcss.common.j17common.EnumSupport;
import com.benshou.bcss.common.j17common.annotation.NotNull;
import com.benshou.bcss.common.j17common.annotation.Nullable;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
import lombok.NonNull;

/**
 * 数据库实体类：系统权限
 *
 * @author <a href="mailto:liuzhenbang@benshou.com">Liu Zhenbang</a>
 */
@Data
public class SysPermission extends BaseEntity {

    /**
     * 权限编号，用于在业务上唯一标识权限
     */
    @TableId(type = IdType.INPUT)
    @TableField(value = "permission_no", fill = FieldFill.INSERT)
    @BusinessNoLong
    @NotNull
    private Long permissionNo;

    /**
     * 权限编码，用于在程序中唯一标识权限，例如：DELETE_USER
     */
    @TableField("permission_code")
    @NotNull
    private String permissionCode;

    /**
     * 权限规则值，用于定义权限规则的具体行为模式，例如：{"type":"DATA","filter":"TO_BE_DESIGNED"}
     */
    @TableField("permission_rule")
    @NotNull
    private JsonNode permissionRule;

    /**
     * 权限规则类型，例如：DATA
     *
     * @see PermissionRuleType#name()
     */
    @TableField("permission_rule_type")
    @NotNull
    private PermissionRuleType permissionRuleType;

    public enum PermissionRuleType {
        /**
         *
         */
        FUNC,
        DATA,
        BIZ,
        ;

        @Nullable
        public static PermissionRuleType getByName(@NonNull String name) {
            PermissionRuleType.valueOf(name);
            return EnumSupport.findByName(PermissionRuleType.class, name);
        }
    }
}
